package com.example.demo.repository;

import com.example.demo.bean.OauthTestInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OauthService {

    public List<OauthTestInfo> oauthInfo(String env) {
        List<OauthTestInfo> infoList = new ArrayList<>();
        try {
            ClassPathResource resource = new ClassPathResource("static/oauth_test.json");
            File file = resource.getFile();
            JsonReader jsonReader = new JsonReader(new FileReader(file));
            List<OauthTestInfo> baseInfo = new Gson().fromJson(jsonReader, new TypeToken<List<OauthTestInfo>>() {
            }.getType());
            if(StringUtils.isNoneBlank(env)){
                infoList = baseInfo.stream().filter(x -> env.equalsIgnoreCase(x.getEnv())).collect(Collectors.toList());
            }else {
                infoList = baseInfo;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return infoList;
    }
}
